let dlzc = document.querySelector('.dlzc')
let zc = document.querySelector('.zc')
let dl = document.querySelector('.dl')
let zcbtn = document.querySelector(".zcbtn")
let dlbtn = document.querySelector(".dlbtn")
let phone = document.querySelector('.phone')
let yzm = document.querySelector('.yzm')
let hqyzm = document.querySelector('.hqyzm')
let pwd = document.querySelector('.pwd')

function gozero(){
    phone.value = ''
    yzm.value = ''
    hqyzm.value = '获取验证码'
    pwd.value = ''
}
function orPhone(){
    let reg = /(^1[3|4|5|6|9][0-9]{9}$)/
    if(!reg.test(phone.value)){
        phone.classList.add('error')
        return true
    }else{
        phone.classList.remove('error')
        return false
    }
}
function orpwd(){
    if(!(pwd.value.trim())){
        pwd.classList.add('error')
        return true
    }else{
        pwd.classList.remove('error')
        return false
    }
}

dlzc.addEventListener('touchend',function(e){
    if(e.target.value == '登录'){
        zc.classList.remove('active')
        dl.classList.add('active')
        zcbtn.classList.remove('bActive')
        dlbtn.classList.add('bActive')
        gozero()
    }else if(e.target.value == '注册'){
        dl.classList.remove('active')
        zc.classList.add('active')
        dlbtn.classList.remove('bActive')
        zcbtn.classList.add('bActive')
        gozero()
    }
})

hqyzm.addEventListener('touchend',function(){
    let index = 4
    let arr = '1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM'
    let sum = ''
    
    for(let i = 0;i<index;i++){
        sum +=  arr[Math.floor(Math.random()*arr.length)]
    }
    localStorage.setItem('key',sum)
    hqyzm.value = localStorage.getItem('key')
})

phone.addEventListener('blur',orPhone)

pwd.addEventListener('blur',orpwd)

zcbtn.addEventListener('touchend',function(){
    if(orPhone()){
        alert('手机号码错误')
        gozero()
        return
    }
    if(orpwd()){
        alert('密码至少一位')
        gozero()
        return
    }
    if(!(yzm.value.toUpperCase() == localStorage.getItem('key').toUpperCase())){
        alert('验证码错误')
        gozero()
        return
    }
    let username = phone.value
    let password =pwd.value 
    user = {
        "username":username,
        "password":password
    }
    localStorage.setItem('user',JSON.stringify(user))
})

dlbtn.addEventListener('touchend',function(){
    if(orPhone()){
        alert('手机号码错误')
        gozero()
        return
    }
    if(orpwd()){
        alert('密码至少一位')
        gozero()
        return
    }
    if(!(yzm.value.toUpperCase() == localStorage.getItem('key').toUpperCase())){
        alert('验证码错误')
        gozero()
        return
    }
    let user = JSON.parse(localStorage.getItem('user'))
    if(phone.value == user.username && pwd.value == user.password){
        alert('登录成功')
    }else{
        alert('登录失败')
    }
})